package com.zjzywl.smoke.authenticate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.zjzywl.smoke.config.AppConfig;
import com.zjzywl.smoke.entity.Authority;
import com.zjzywl.smoke.entity.SysUser;

@Component
public class CustomUserDetailsService implements UserDetailsService {
	@Autowired
	private AppConfig appConfig;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		List<SimpleGrantedAuthority> roles = new ArrayList<SimpleGrantedAuthority>();
		ResponseEntity<SysUser> people = appConfig.restTemplate()
				.getForEntity(appConfig.getGetDataIp() + "/people/" + username, SysUser.class);
		ResponseEntity<Authority> authority = appConfig.restTemplate()
				.getForEntity(appConfig.getGetDataIp() + "/username/" + username, Authority.class);
		if (authority != null) {
			if (authority.getBody().getManage() != null && authority.getBody().getManage().equals("1")) {
				roles.add(new SimpleGrantedAuthority("manage"));
			}
			if (authority.getBody().getFront() != null && authority.getBody().getFront().equals("1")) {
				roles.add(new SimpleGrantedAuthority("front"));
			}
			if (authority.getBody().getPatrolManagement() != null
					&& authority.getBody().getPatrolManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("patrolManagement"));
			}
			if (authority.getBody().getPatrolPoint() != null && authority.getBody().getPatrolPoint().equals("1")) {
				roles.add(new SimpleGrantedAuthority("patrolPoint"));
			}
			if (authority.getBody().getPatrolPersonnel() != null
					&& authority.getBody().getPatrolPersonnel().equals("1")) {
				roles.add(new SimpleGrantedAuthority("patrolPersonnel"));
			}
			if (authority.getBody().getDailyManagement() != null
					&& authority.getBody().getDailyManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("dailyManagement"));
			}
			if (authority.getBody().getUnitException() != null && authority.getBody().getUnitException().equals("1")) {
				roles.add(new SimpleGrantedAuthority("unitException"));
			}
			if (authority.getBody().getAlarmManagement() != null
					&& authority.getBody().getAlarmManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("alarmManagement"));
			}
			if (authority.getBody().getHistoricalInformation() != null
					&& authority.getBody().getHistoricalInformation().equals("1")) {
				roles.add(new SimpleGrantedAuthority("historicalInformation"));
			}
			if (authority.getBody().getBasicConfiguration() != null
					&& authority.getBody().getBasicConfiguration().equals("1")) {
				roles.add(new SimpleGrantedAuthority("basicConfiguration"));
			}
			if (authority.getBody().getDeviceManagement() != null
					&& authority.getBody().getDeviceManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("deviceManagement"));
			}
			if (authority.getBody().getAccountManagement() != null
					&& authority.getBody().getAccountManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("accountManagement"));
			}
			if (authority.getBody().getFireStationManagement() != null
					&& authority.getBody().getFireStationManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("fireStationManagement"));
			}
			if (authority.getBody().getOrganizationManagement() != null
					&& authority.getBody().getOrganizationManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("organizationManagement"));
			}
			if (authority.getBody().getDataStatistics() != null
					&& authority.getBody().getDataStatistics().equals("1")) {
				roles.add(new SimpleGrantedAuthority("dataStatistics"));
			}
			if (authority.getBody().getPersonnelManagement() != null
					&& authority.getBody().getPersonnelManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("personnelManagement"));
			}
			if (authority.getBody().getRoleManagement() != null
					&& authority.getBody().getRoleManagement().equals("1")) {
				roles.add(new SimpleGrantedAuthority("roleManagement"));
			}
			if (authority.getBody().getDeviceStatistics() != null
					&& authority.getBody().getDeviceStatistics().equals("1")) {
				roles.add(new SimpleGrantedAuthority("deviceStatistics"));
			}
			if (authority.getBody().getPersonnelStatistics() != null
					&& authority.getBody().getPersonnelStatistics().equals("1")) {
				roles.add(new SimpleGrantedAuthority("personnelStatistics"));
			}
			if (authority.getBody().getAlarmUnprocessed() != null
					&& authority.getBody().getAlarmUnprocessed().equals("1")) {
				roles.add(new SimpleGrantedAuthority("alarmUnprocessed"));
			}
			if (authority.getBody().getPatrolStatistics() != null
					&& authority.getBody().getPatrolStatistics().equals("1")) {
				roles.add(new SimpleGrantedAuthority("patrolStatistics"));
			}
			if (authority.getBody().getModifiable() != null && authority.getBody().getModifiable().equals("1")) {
				roles.add(new SimpleGrantedAuthority("modifiable"));
			}
			if (authority.getBody().getKeepWatch() != null && authority.getBody().getKeepWatch().equals("1")) {
				roles.add(new SimpleGrantedAuthority("keepWatch"));
			}
//			if (authority.getBody().getWindow() != null && authority.getBody().getWindow().equals("1")) {
//                roles.add(new SimpleGrantedAuthority("window"));
//            }
			if (authority.getBody().getThreeColor() != null && authority.getBody().getThreeColor().equals("1")) {
			    roles.add(new SimpleGrantedAuthority("threeColor"));
			}
			if (authority.getBody().getKeyUnit() != null && authority.getBody().getKeyUnit().equals("1")) {
			    roles.add(new SimpleGrantedAuthority("keyUnit"));
			}
			if (authority.getBody().getHouseMaster() != null && authority.getBody().getHouseMaster().equals("1")) {
			    roles.add(new SimpleGrantedAuthority("houseMaster"));
			}
			if (authority.getBody().getCombinedAction() != null && authority.getBody().getCombinedAction().equals("1")) {
			    roles.add(new SimpleGrantedAuthority("combinedAction"));
			}if (authority.getBody().getSubplatformList() != null && authority.getBody().getSubplatformList().equals("1")) {
                roles.add(new SimpleGrantedAuthority("subplatformList"));
            }
            if (authority.getBody().getBuildingDetection() != null && authority.getBody().getBuildingDetection().equals("1")) {
                roles.add(new SimpleGrantedAuthority("buildingDetection"));
            }
            if (authority.getBody().getOilFumeDetection() != null && authority.getBody().getOilFumeDetection().equals("1")) {
                roles.add(new SimpleGrantedAuthority("oilFumeDetection"));
            }
            if (authority.getBody().getCulturalRelicProtection() != null && authority.getBody().getCulturalRelicProtection().equals("1")) {
                roles.add(new SimpleGrantedAuthority("culturalRelicProtection"));
            }
            if (authority.getBody().getCombinedAction() != null && authority.getBody().getCombinedAction().equals("1")) {
                roles.add(new SimpleGrantedAuthority("combinedAction"));
            }
            if (authority.getBody().getPartyBuilding() != null && authority.getBody().getPartyBuilding().equals("1")) {
                roles.add(new SimpleGrantedAuthority("partyBuilding"));
            }
            if (authority.getBody().getHbjd() != null && authority.getBody().getHbjd().equals("1")) {
                roles.add(new SimpleGrantedAuthority("hbjd"));
            }
            if (authority.getBody().getXyjd() != null && authority.getBody().getXyjd().equals("1")) {
                roles.add(new SimpleGrantedAuthority("xyjd"));
            }
			if (authority.getBody().getInformationMaintenance() != null && authority.getBody().getInformationMaintenance().equals("1")) {
				roles.add(new SimpleGrantedAuthority("nformationMaintenance"));
				
			}
			return new AuthUser(people.getBody(), roles);
		}
		return null;
	}
}
